package com.liujie.dhunting.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liujie.dhunting.entity.Resume;
import com.liujie.dhunting.mapper.ResumeMapper;
import com.liujie.dhunting.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@RequestMapping("/resume")
@RestController
public class ResumeController {

    @Resource
    ResumeMapper resumeMapper;

//    @GetMapping("/find")
//    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNumber,
//                              @RequestParam(defaultValue = "10") Integer pageSize){
//        LambdaQueryWrapper<Resume> wrapper= Wrappers.<Resume>lambdaQuery();
//        wrapper.isNull(Resume::getAdid);
//        Page<Resume> resumePage = resumeMapper.selectPage(new Page<Resume>(pageNumber,pageSize), wrapper);
//        return Result.success(resumePage);
//    }
    @GetMapping("/Myfind")
    public Result<?> findPage(@RequestParam (defaultValue = "") String adid,
                              @RequestParam (defaultValue = "0") Integer model){
        LambdaQueryWrapper<Resume> wrapper= Wrappers.<Resume>lambdaQuery();
        wrapper.eq(Resume::getAdid,adid);
        List<Resume> resumeList;
        if(!model.equals(0)){
            if(model.equals(1)){
                wrapper.eq(Resume::isMine,true);
            }else if(model.equals(2)){
                wrapper.eq(Resume::isMine,false);
            }else{
                return Result.error();
            }
        }
        resumeList=resumeMapper.selectList(wrapper);
        return Result.success(resumeList);
    }

    @GetMapping("/progress")
    public Result<?> progress(@RequestParam(defaultValue = "1")Integer pageNumber,
                              @RequestParam(defaultValue = "10")Integer pageSize){
        LambdaQueryWrapper<Resume> wrapper= Wrappers.<Resume>lambdaQuery();
        wrapper.ne(Resume::getAdid,"新增")
                .ne(Resume::getAdid,"offer")
                .ne(Resume::getAdid,"入职")
                .ne(Resume::getAdid,"过保");
        Page<Resume> page=resumeMapper.selectPage(new Page<Resume>(pageNumber,pageSize),wrapper);
        return Result.success(page);
    }

    @PostMapping("/addByAdmin")
    public Result<?> addByAdmin(@RequestBody Resume resume){
        LambdaQueryWrapper<Resume> wrapper=Wrappers.lambdaQuery();
        wrapper.orderByDesc(Resume::getResumeid);
        resume.setResumeid(resumeMapper.selectList(wrapper).get(0).getResumeid()+1);
        if(resume.getRstate()==null){
            resume.setRstate("新增");
        }

        int flag=resumeMapper.insert(resume);
        if(flag==1){
            return Result.success();
        }else{
            return Result.error();
        }
    }

    @GetMapping("/search")
    public Result<?> search(@RequestParam Integer pageNumber,
                            @RequestParam Integer pageSize,
                            @RequestParam(defaultValue = "") String keyWord,
                            @RequestParam(defaultValue = "") String gender,
                            @RequestParam(defaultValue = "-1") Integer ageBottom,
                            @RequestParam(defaultValue = "1000") Integer ageTop,
                            @RequestParam(defaultValue = "") Integer salarye,
                            @RequestParam(defaultValue = "") Integer salaryn,
                            @RequestParam(defaultValue = "") String inde
                            ){
        LambdaQueryWrapper<Resume> wrapper = Wrappers.lambdaQuery();
        wrapper.between(Resume::getAge,ageBottom,ageTop);
        wrapper.isNull(Resume::getAdid);
        if(!gender.equals("")){
            wrapper.eq(Resume::getGender,gender);
        }
        if(salarye!=null){
            wrapper.ge(Resume::getSalarye,salarye);
        }
        if(salaryn!=null){
            wrapper.ge(Resume::getSalaryn,salaryn);
        }
        if(!inde.equals("")){
            wrapper.like(Resume::getInde,inde);
        }
        if(!keyWord.equals("")){
            wrapper.like(Resume::getInde,keyWord).or()
                    .like(Resume::getResumename,keyWord).or()
                    .like(Resume::getRstate,keyWord).or()
                    .like(Resume::getPosition,keyWord).or()
                    .like(Resume::getAddress,keyWord).or()
                    .like(Resume::getCity,keyWord).or()
                    .like(Resume::getLanguage,keyWord).or()
                    .like(Resume::getIndn,keyWord).or()
                    .like(Resume::getUniversity,keyWord).or()
                    .like(Resume::getMajor,keyWord);
        }

        Page<Resume> resumePage=resumeMapper.selectPage(new Page<>(pageNumber,pageSize),wrapper);
        return Result.success(resumePage);
    }
    @GetMapping("/select")
    public Result<?> select(@RequestParam String rstate,
                            @RequestParam Date time1,
                            @RequestParam Date time2){
        LambdaQueryWrapper<Resume> wrapper=Wrappers.lambdaQuery();
        wrapper.between(Resume::getTime,time1,time2)
                .eq(Resume::getRstate,rstate);
        List<Resume> resumeList=resumeMapper.selectList(wrapper);
        return Result.success(resumeList);
    }


}
